<?php


namespace api\modules\app\services;


use api\components\Common;
use api\modules\app\models\MetaArea;
use api\modules\app\models\Qrcode;
use api\modules\app\models\Section;
use api\modules\app\models\Unit;
use Yii;
use yii\helpers\ArrayHelper;

class DashboardService
{

    public static function search($params)
    {
        /**  参数获取 */
        $districtCode = '06';

        /** 部件统计 */
        $totalCount = Unit::find()->andFilterWhere(['district_code' => $districtCode, 'status' => 1])->count();
        $bindCount = Unit::find()->andFilterWhere(['district_code' => $districtCode, 'status' => 1, 'has_bind' => 1])->count();
        $badCount = Unit::find()->andFilterWhere(['district_code' => $districtCode, 'status' => 1, 'state' => 2])->count();
        $bindRate = $totalCount == 0 ? '0%' : (string)round(($bindCount * 100)/$totalCount,2) . '%';
        $badRate = $totalCount == 0 ? '0%' : (string)round(($badCount * 100)/$totalCount,2) . '%';
        $overview = [
            'total' => $totalCount,
            'bind' => $bindCount,
            'bad' => $badCount,
            'bind_rate' => $bindRate,
            'bad_rate' => $badRate,
        ];

        /** 街道明细 */
        $streets = MetaArea::findAll(['category' => '街道','parent_id' => 6]);

        $sql = "SELECT COUNT(1) as 'count',street_code FROM unit WHERE `district_code` = '06' AND `status`=1 GROUP BY `street_code`";
        $query = \Yii::$app->test_3->createCommand($sql);
        $totalData = $query->queryAll();
        $totalData = ArrayHelper::map($totalData,'street_code','count');
//        \Yii::error('dashboard:'.json_encode($totalData));

        $sql = "SELECT COUNT(1) as 'count',street_code FROM unit WHERE `district_code` = '06' AND `status`=1 AND `has_bind`=1 GROUP BY `street_code`";
        $query = \Yii::$app->test_3->createCommand($sql);
        $bindData = $query->queryAll();
        $bindData = ArrayHelper::map($bindData,'street_code','count');
//        \Yii::error('dashboard:'.json_encode($bindData));

        $sql = "SELECT COUNT(1) as 'count',street_code FROM unit WHERE `district_code` = '06' AND `status`=1 AND `state`=2 GROUP BY `street_code`";
        $query = \Yii::$app->test_3->createCommand($sql);
        $badData = $query->queryAll();
        $badData = ArrayHelper::map($badData,'street_code','count');
//        \Yii::error('dashboard:'.json_encode($badData));

        $tableData = [];
        if (!empty($streets)){
            $i = 1;
            foreach ($streets as $street){
                $rowData = [];
                $streetCode = $street->code;
                $streetName = $street->name;
                $rowTotal = isset($totalData[$streetCode]) ? intval($totalData[$streetCode]) : 0;
                $rowBind = isset($bindData[$streetCode]) ? intval($bindData[$streetCode]) : 0;
                $rowBad = isset($badData[$streetCode]) ? intval($badData[$streetCode]) : 0;
                $rowData = [
                    'sn' => $i++,
                    'street' => $streetName,
                    'total' => $rowTotal,
                    'bind' => $rowBind,
                    'bad' => $rowBad,
                    'bindRate' => $rowTotal == 0 ? '0%' : (string)round(($bindData[$streetCode] * 100)/$rowTotal,2) . '%',
                    'badRate' => $rowTotal == 0 ? '0%' : (string)round(($badData[$streetCode] * 100)/$rowTotal,2) . '%'
                ];
                $tableData[] = $rowData;
            }
        }

        return [
            'overview' => $overview,
            'tableData' => $tableData
        ];
    }
}